package hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * @author OnCloud9
 * @description
 * @project QQ-Robot
 * @date 2022年07月02日 下午 02:03
 */
public class HdfsClient {

    /**
     * 获取HDFS，
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @Test
    public void testMkdirs() throws
            IOException,
            URISyntaxException,
            InterruptedException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        // FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration);
        FileSystem fs = FileSystem
                .get(new URI("hdfs://Centos8-1:8020"), configuration,"root");
        // 2 创建目录
        fs.mkdirs(new Path("/xiyou/huaguoshan/"));
        // 3 关闭资源
        fs.close();
    }

    /**
     * 把本地资源上传到HDFS
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testCopyFromLocalFile() throws
            IOException,
            InterruptedException,
            URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "2");
        FileSystem fs = FileSystem
                .get(new URI("hdfs://Centos8-1:8020"), configuration, "root");
        // 2 上传文件
        fs.copyFromLocalFile(
                new Path("C:\\Users\\Administrator\\Pictures\\QQ截图20220702134109.png"),
                new Path("/xiyou/huaguoshan"));
        // 3 关闭资源
        fs.close();
    }

    /**
     * 把HDFS资源下载到本地
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testCopyToLocalFile() throws
            IOException,
            InterruptedException,
            URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(
                new URI("hdfs://Centos8-1:8020"),
                configuration, "root");

        // 2 执行下载操作
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(
                false,
                new Path("/xiyou/huaguoshan/QQ截图20220702134109.png"),
                new Path("C:\\Users\\Administrator\\Desktop\\返回的图片.png"), true);

        // 3 关闭资源
        fs.close();
    }

    /**
     * 更改HDFS存储的文件名
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testRename() throws
            IOException,
            InterruptedException,
            URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://Centos8-1:8020"),
                configuration, "root");
        // 2 修改文件名称
        fs.rename(
                new Path("/xiyou/huaguoshan/QQ截图20220702134109.png"),
                new Path("/xiyou/huaguoshan/qqSnapShot-1001.png"));
        // 3 关闭资源
        fs.close();
    }

    /**
     * 调用HDFS 获取目录信息
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testListFiles() throws
            IOException,
            InterruptedException,
            URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem
                .get(new URI("hdfs://Centos8-1:8020"), configuration, "root");
        // 2 获取文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(
                new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("========" + fileStatus.getPath() + "=========");
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPath().getName());
            // 获取块信息
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
        }
        // 3 关闭资源
        fs.close();
    }


    /**
     * 调用HDFS 删除 path
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testDelete() throws
            IOException,
            InterruptedException,
            URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://Centos8-1:8020"), configuration, "root");
        // 2 执行删除
        fs.delete(new Path("/xiyou"), true);
        // 3 关闭资源
        fs.close();
    }

    @Test
    public void testListStatus() throws IOException, InterruptedException,
            URISyntaxException{
        // 1 获取文件配置信息
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://Centos8-1:8020"), configuration, "root");
        // 2 判断是文件还是文件夹
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("file:"+fileStatus.getPath().getName());
            }else {
                System.out.println("direct:"+fileStatus.getPath().getName());
            }
        }
        // 3 关闭资源
        fs.close();
    }





}
